package com.aoxon.cargo.shared;

import com.aoxon.cargo.bean.Index;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.cache.keyCache;
import com.aoxon.cargo.util.MD5;

/**
 * 管理供应商本地登陆数据的保存
 * 
 * @author ZZJ
 * 
 */
public class SupIndexShared extends MyShared {

	/**
	 * 保存供应商个人信息
	 * 
	 * @param strSupplier
	 */
	public static void saveSupplier(String strSupplier) {
		saveDate("Supplier", strSupplier);
	}

	/**
	 * 获取本地保存的供应商信息
	 * 
	 * @return
	 */
	public static Supplier getSupplier() {
		// 获取本地存储的用户登录信息
		try {
			String strSupplier = sp.getString("Supplier", "");
			Supplier supplier = gson.fromJson(strSupplier, Supplier.class);
			return supplier;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 保存验证的加密密匙
	 * 
	 * @param key
	 */
	public static void saveKey(String key) {
		saveDate("Key", key);
	}

	/**
	 * 获取验证用的加密密匙
	 * 
	 * @return
	 */
	public static String getKey() {
		return sp.getString("Key", "");
	}

	/**
	 * 保存登陆的供应商信息和密匙
	 * 
	 * @param strSupplier
	 */
	public static void saveSupplierAndKey(String strSupplier) {
		Supplier supplier = gson.fromJson(strSupplier, Supplier.class);
		String key = new MD5().toKey(supplier);
		saveSupplier(strSupplier);
		saveKey(key);
		keyCache.supId = supplier.getSupId();
		keyCache.key = key;
	}

	/**
	 * 保存供应商首页的数据
	 * 
	 * @param index
	 */
	public static void saveSupplierIndex(Index index) {
		editor = sp.edit();
		saveDate("IndexData", gson.toJson(index));
		saveSupplier(gson.toJson(index.getSupplier()));
	}

	/**
	 * 获取供应商首页的本地数据
	 * 
	 * @return
	 */
	public static Index getIndex() {
		Index index = null;
		String strIndex = sp.getString("IndexData", "");
		if (strIndex != null && !strIndex.equals("none")) {
			index = gson.fromJson(strIndex, Index.class);
		}
		return index;
	}

}
